home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Random2.0 / Source / StandardEngine.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  128 lines

  1. //
  2. // StandardEngine
  3. //
  4. // Copyright (C) 1992 Contemporary Design Studios. All rights reserved.
  5. //
  6.  
  7.  
  8. #import "StandardEngine.h"
  9. #import <sys/time.h>
  10. #import <stdlib.h>
  11.  
  12.  
  13. @implementation StandardEngine
  14.  
  15.  
  16. //
  17. // unit
  18. //
  19. // The standard random call returns 31 pseudo-random bits in a 32-bit signed long.
  20. // We want every byte in our buffer to have 8 pseudo-random bits. So, we generate
  21. // one word of 31 random bits, and then generate 31 more words (each with 31
  22. // pseudo-random bits), and to each we add a bit from the first to bring the total
  23. // in each word to 32. This means we must create a total of 31 words for each call
  24. // to makeRandom, so our unit size is 31 * 4, or 124.
  25. //
  26.  
  27. + (int)unit
  28. {
  29.     return 124;
  30. }
  31.  
  32.  
  33. //
  34. // init
  35. //
  36.  
  37. - init
  38. {
  39.     struct timeval theTime;
  40.     gettimeofday(&theTime, 0);
  41.     last = theTime.tv_usec;
  42.     srand(last);
  43.     
  44.     return self;
  45. }
  46.  
  47.  
  48. //
  49. // makeRandom:
  50. //
  51.  
  52. - makeRandom:(uchar *)storage;
  53. {
  54.     ulong        temp;
  55.     int            i;
  56.     ulong        *out = (ulong *)storage;
  57.     
  58.     temp = rand();
  59.     
  60.     for(i = 0; i < 31; i++) {
  61.         out[i] = rand() + ((temp & 0x00000001) ? 0x80000000 : 0x00000000);
  62.     temp = temp >> 1;
  63.     }
  64.     
  65.     last = out[30];
  66.     
  67.     
  68.     ((uchar *)storage)[ 0] = (last & 0x000000ff);
  69.     ((uchar *)storage)[ 1] = (last & 0x0000ff00) >> 8;
  70.     ((uchar *)storage)[ 2] = (last & 0x00ff0000) >> 16;
  71.  
  72.     last = rand();
  73.     
  74.     ((uchar *)storage)[ 3] = (last & 0x000000ff);
  75.     ((uchar *)storage)[ 4] = (last & 0x0000ff00) >> 8;
  76.     ((uchar *)storage)[ 5] = (last & 0x00ff0000) >> 16;
  77.  
  78.     last = rand();
  79.     
  80.     ((uchar *)storage)[ 6] = (last & 0x000000ff);
  81.     ((uchar *)storage)[ 7] = (last & 0x0000ff00) >> 8;
  82.     ((uchar *)storage)[ 8] = (last & 0x00ff0000) >> 16;
  83.  
  84.     last = rand();
  85.     
  86.     ((uchar *)storage)[ 9] = (last & 0x000000ff);
  87.     ((uchar *)storage)[10] = (last & 0x0000ff00) >> 8;
  88.     ((uchar *)storage)[11] = (last & 0x00ff0000) >> 16;
  89.     
  90.     return self;
  91. }
  92.  
  93.  
  94. //
  95. // read:
  96. //
  97.  
  98. - read:(NXTypedStream *)stream
  99. {
  100.     [super write:stream];
  101.     
  102.     NXReadTypes(stream, "i", &last);
  103.     srand(last);
  104.     
  105.     return self;
  106. }
  107.  
  108.  
  109. //
  110. // write:
  111. //
  112.  
  113. - write:(NXTypedStream *)stream
  114. {
  115.     [super write:stream];
  116.     
  117.     NXWriteTypes(stream, "i", &last);
  118.     
  119.     return self;
  120. }
  121.  
  122.  
  123. @end
  124.  
  125.  
  126. //
  127. // End of file.
  128. //